package com.sun.rocketmq;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Demo1Producer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 同步发送消息
     * @param id
     * @return
     */
    public SendResult syncSend(Integer id){
        Demo1Message message = new Demo1Message();
        message.setId(id);
        return rocketMQTemplate.syncSend(Demo1Message.TOPIC, message);
    }

    public void asyncSend(Integer id, SendCallback callback){
        Demo1Message message = new Demo1Message();
        message.setId(id);
        rocketMQTemplate.asyncSend(Demo1Message.TOPIC, message, callback);
    }

    public void onewaySend(Integer id) {
        // 创建 Demo01Message 消息
        Demo1Message message = new Demo1Message();
        message.setId(id);
        // oneway 发送消息
        rocketMQTemplate.sendOneWay(Demo1Message.TOPIC, message);
    }


}
